Skip to content

bpo-46993: Speed up bytearray creation from list and tuple #31834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 15, 2022

Conversation

kumaraditya303
Copy link
Contributor

@kumaraditya303 kumaraditya303 commented Mar 12, 2022

Speeds up bytearray creation from list and tuples.

Benchmark:

import pyperf

runner = pyperf.Runner()
runner.timeit(name="bench bytearray",
              stmt="bytearray([42]*10000)",)

Result:

Mean +- std dev: [base] 74.8 us +- 5.5 us -> [patch] 53.2 us +- 3.3 us: 1.41x faster

https://bugs.python.org/issue46993

@kumaraditya303 kumaraditya303 changed the title optimize bytearray for list and tuple bpo-46993: Speed up bytearray creation from list and tuple Mar 12, 2022
@kumaraditya303 kumaraditya303 marked this pull request as ready for review March 12, 2022 09:41
@kumaraditya303 kumaraditya303 marked this pull request as draft March 13, 2022 06:30
@kumaraditya303 kumaraditya303 marked this pull request as ready for review March 13, 2022 07:28
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, much better.

I suppose there's a way to observe the new behavior, if the jump to the slow path must be taken: the offending subtype-of-long-instance will see that its __index__ method is called twice. But I see no reason this would matter.

@asvetlov asvetlov merged commit 6dfe09f into python:main Mar 15, 2022
@kumaraditya303 kumaraditya303 deleted the bytearray branch March 15, 2022 13:28
@kumaraditya303
Copy link
Contributor Author

Thanks @asvetlov and @gvanrossum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants